import {
  createRouter,
  createWebHashHistory,
  createWebHistory
} from 'vue-router'
import Main from '../views/main.vue'
import mainPage from '../views/mainPage.vue'
import personalCenter from '../views/personalCenter.vue'
import loginPage from '../views/login/login.vue'
import registerPage from '../views/register/register.vue'
import diaryListPage from '../views/diary/diary.vue'
import diarySpace from '../views/diary/diarySpace.vue'
import diaryDetail from '../views/diary/diaryDetail.vue'
import userPersonalMainPage from '../views/userPersonalMainPage.vue'
import userConcern from '../views/user/userConcern.vue'
import otherUserDiaryList from '../views/diary/otherUserDiaryList.vue'
import otherUserBlogList from '../views/blog/otherBlogList.vue'
import diaryRecommend from '../views/diary/diaryRecommend.vue'
import concernUserDiary from '../views/diary/concernUserDiary.vue'
import diaryEditOrAdd from '../views/diary/diaryEditOrAdd.vue'
import myCollectionPage from '../views/user/myCollectionPage.vue'
import myInformation from '../views/user/myInfomation.vue'
import messageCenter from '../views/user/messageCenter.vue'
import about from '../views/user/about.vue'
import setting from '../views/user/setting.vue'
import userChat from '../views/user/userChatPage.vue'
import myblog from '../views/blog/myblog.vue'
import blogDetail from '../views/blog/blogDetail.vue'
import blogEditOrAdd from '../views/blog/blogEditOrAdd.vue'
// import spaceBlog from '../views/blog/spaceBlog.vue'
// import blogRecommend from '../views/blog/blogRecommend.vue'
// import blogConcern from '../views/blog/blogConcern.vue'
import {
  checkTokenIsExpired
} from '../api/allSituation'
import {
  Toast
} from 'vant'

const spaceBlog = () => import('../views/blog/spaceBlog');
const blogRecommend = () => import('../views/blog/blogRecommend');
const blogConcern = () => import('../views/blog/blogConcern');
const recommendSongList = () => import('../views/music/recommendSongList');
const listSong = () => import('../views/music/listSong');
const song = () => import('../views/music/song')


const routes = [{
    path: '/',
    name: 'Main',
    component: Main,
    redirect: '/mainPage',
    children: [{
        path: '/mainPage',
        name: 'mainPage',
        component: mainPage,
      },
      {
        // 我的（个人中心页面）
        path: '/personalCenter',
        name: 'personalCenter',
        component: personalCenter,
      },
    ],
  },
  {
    // 登陆页面
    path: '/login',
    name: 'loginPage',
    component: loginPage,
  },
  {
    // 注册页面
    path: '/register',
    name: 'registerPage',
    component: registerPage,
  },
  {
    // （我的）日记列表页面
    path: '/diary',
    name: 'diary',
    component: diaryListPage,
  },
  {
    // （其他人）日记列表页面
    path: '/otherUserDiaryList',
    name: 'otherUserDiaryList',
    component: otherUserDiaryList,
  },
  {
    // （其他人）博客列表页面
    path: '/otherUserBlogList',
    name: 'otherBlogList',
    component: otherUserBlogList,
  },
  {
    // 编辑或添加日记页面
    path: '/diaryEditOrAdd',
    name: 'diaryEditOrAdd',
    component: diaryEditOrAdd,
  },
  {
    // 空间日记
    path: '/diarySpace',
    name: 'diarySpace',
    component: diarySpace,
    redirect: '/diarySpace/recommend',
    children: [{
        // 推荐
        path: '/diarySpace/recommend',
        name: 'diaryRecommend',
        component: diaryRecommend,
      },
      {
        // 关注
        path: '/diarySpace/concern',
        name: 'concernUserDiary',
        component: concernUserDiary,
      },
    ],
  },
  {
    // 日记详情页面
    path: '/diaryDetail',
    name: 'diaryDetail',
    component: diaryDetail,
  },
  {
    // 别的用户的个人主页
    path: '/userPersonalMainPage',
    name: 'userPersonalMainPage',
    component: userPersonalMainPage,
  },
  {
    // 用户关注和被关注页面
    path: '/userConcern',
    name: 'userConcern',
    component: userConcern,
  },
  {
    // 我的收藏页面
    path: '/myCollectionPage',
    name: 'myCollectionPage',
    component: myCollectionPage,
  },
  {
    // 我的资料页面
    path: '/myInfomation',
    name: 'myInformation',
    component: myInformation,
  },
  {
    // 消息中心页面
    path: '/messageCenter',
    name: 'messageCenter',
    component: messageCenter,
  },
  {
    // 关于页面
    path: '/about',
    name: 'about',
    component: about,
  },

  {
    // 设置页面
    path: '/setting',
    name: 'setting',
    component: setting,
  },

  {
    // 用户聊天页面
    path: '/userChat',
    name: 'userChat',
    component: userChat,
  },
  {
    // 我的博客页面
    path: '/blog/myblog',
    name: 'myblog',
    component: myblog,
  },
  {
    // 博客详情页面
    path: '/blog/blogDetail',
    name: 'blogDetail',
    component: blogDetail,
  },
  {
    // 博客编辑或添加页面
    path: '/blog/blogEditOrAdd',
    name: 'blogEditOrAdd',
    component: blogEditOrAdd,
  },
  {
    // 博客空间
    path: '/blog/blogSpace',
    name: 'spaceBlog',
    component: spaceBlog,
    redirect: '/blog/blogSpace/blogRecommend',
    children: [{
        path: '/blog/blogSpace/blogRecommend',
        name: 'blogRecommend',
        component: blogRecommend,
      },
      {
        path: '/blog/blogSpace/blogConcern',
        name: 'blogConcern',
        component: blogConcern,
      }
    ]
  },
  {
    // 推荐歌单页面
    path: '/music/recommendSongList',
    name: 'recommendSongList',
    component: recommendSongList,
  },
  {
    // 歌单的歌曲页面
    path: '/music/songList/song',
    name: 'listSong',
    component: listSong,
  },
  {
    // 歌曲播放主页面
    path: '/music/song',
    name: 'song',
    component: song,
  }
]

const router = createRouter({
  // mode: "hash",
  history: createWebHashHistory(process.env.BASE_URL),
  routes
})

//路由拦截 我们经常使⽤路由守卫实现⻚⾯的鉴权
router.beforeEach((to, from, next) => {
  // 如果token过期了
  checkTokenIsExpired().then(res => {
    if (res.data.msg === 'token已过期！' && res.data.state === false) {
      localStorage.removeItem('token');
      Toast({
        message: '登陆失效了，请重新登陆！',
        duration: 1000,
      })
      setTimeout(() => {
        router.push('/login');
      }, 1000);
    } else if (res.data.msg === '无效token！' && res.data.state === false) {
      // console.log("当前页面url为：" + window.location.href);
      // console.log(window.location.href.search('/register'));
      localStorage.removeItem('token');
      if (window.location.href.search('/register') == -1) {
        // 不是注册页面，直接跳转登陆
        router.push('/login');
      } else {
        router.push('/register');
      }
    }
  })
  next();
})

export default router